$(function () {
    var empdata=$("#empdata")
    var tabsForm=$("#tabsForm")
    var tabsForm
    var transferForm=$("#transferForm")
    var transfer=$("#transfer")
    var lossDiv=$("#lossDiv")
    var lossForm=$("#lossForm")
    empdata.datagrid({
        url:"/studentmanagement/StudentList.do",
        fit:true,
        striped:true,
        sortName:"owe",//默认排序规则
        sortOrder:"desc",
        pagination:true,
        fitColumns:true,
        columns:[[
            {field:"id",checkbox:true},
            {field:"name",title:"学员姓名",width:100},//姓名
            {field:"salesman",title:"营销人员",width:100,formatter: function (value) {
                    return value.username
                }},//营销人员
            {field:"totlMoney",title:"总学费"},//总学费
            {field:"owe",title:"待缴学费",width:100,sortable:true},//待缴学费
            {field:"money",title:"已缴学费",width:100},//已缴学费
            {field:"moneyState",title:"缴费状态",width:100,formatter: function (value,row) {
                    return row.owe?'<font color="red">未缴齐</font>':"<font color='green'>缴齐</font>"
                }},//缴费状态
            {field:"admissionTime",title:"入学时间",width:100},//入学时间
            {field:"school",title:"学校",width:100},//学校
            {field:"tel",title:"电话",width:100},//电话
            {field:"grade",title:"所在班级",width:100,formatter: function (value) {
                    return value?value.className:''
                }},//所在班级
            {field:"type",title:"付款方式",width:100},//付款方式
            {field:"clientType",title:"类型",width:100},//类型
            {field:"state",title:"状态",width:100,formatter: function (value) {
                    return value="正常状态"?'<font color="green">'+value+'</font>':'<font color="red">'+value+'</font>'
            }}//状态
        ]],

        toolbar:"#buttons",
    })
    $("#myDialog").dialog({
        closed:true,
        width: 580,
        height: 420,
        onColse:function () {
            tabsForm.form("clear")
        },
        buttons:"#btns"
    })
    var methodObj={
        edit:function () {
            var row=empdata.datagrid("getSelected");
            if(row) {
                $("#myDialog").dialog({
                    title: '修改'
                })
                if(row.salesman){
                    row["salesman.username"]=row.salesman.username
                }
                if(row.grade){
                    row["className"]=row.grade.className
                }
                row["iDNum"] = row.idnum;
                //回显就业意向复选框的状态
                var empIntention = row.employmentIntention;
                delete row.employmentIntention;
                tabsForm.form('load',row);

                if (empIntention) {
                    var eis = $.parseJSON(empIntention);
                    $.each(eis,function (i,e) {
                        $.each($(".employmentIntention"),function (ind,ele) {
                            var el = $(ele).val();
                            if(e == el){
                                $(ele).prop("checked",true);
                            }
                        });
                    })
                }

                $("#myDialog").dialog("open")
                row["grade.id"]=row.grade.id
                tabsForm.form("load",row)
                tabsForm.form(row)
                $("#tabsForm input").prop("readonly",false);

            }
        },
        refresh:function () {
            empdata.datagrid("reload")
        },
        view:function () {
            var row=empdata.datagrid("getSelected");
            if(row) {
                $("#myDialog").dialog({
                    title: '查看'
                })
                if(row.salesman){
                    row["salesman.username"]=row.salesman.username
                }
                if(row.grade){
                    row["className"]=row.grade.className
                }
                $("#myDialog").dialog("open")
                row["grade.id"]=row.grade.id
                tabsForm.form("load",row)
                $("#tabsForm input").prop("readonly",true)//设置只读
            }
        },
        transfer:function () {
            var row=empdata.datagrid("getSelected");
            if(row) {
                transfer.dialog("open")
                var className = row["grade"]
                transferForm.form("load", className)
                transferForm.form("load", row)
            }else{
                alert("请选择一名学员")
            }
        },
        levae:function () {
            var row=empdata.datagrid("getSelected");
            if(row) {
                var row = empdata.datagrid("getSelected");
                $.post("/studentmanagement/updateState.do", {"id": row.id, "state": "休学状态"}, function () {
                    empdata.datagrid("reload")
                }, "json")
            }else{
                alert("请选择一名学员")
            }
        },
        loss:function (){
            lossDiv.dialog("open")
        },
        query:function () {
            var keeyWord=$("#keyWord").textbox("getValue")
            var startDate=$("#startDate").datebox("getValue")
            var stopDate=$("#stopDate").datebox("getValue")
            var keyClassName=$("#keyClassName").combobox("getValue");
            empdata.datagrid("load",{
                keyWord:keeyWord,
                startDate:startDate,
                stopDate:stopDate,
                keyClassName:keyClassName
            })
        },
        out:function () {
            var keyWord=$("#keyWord").textbox("getValue")
            var startDate=$("#startDate").textbox("getValue")
            var stopDate=$("#stopDate").textbox("getValue")
            var keyClassName=$("#keyClassName").textbox("getValue")
            window.open("/export/exportXLS.do?keyWord="+keyWord+"&startDate="+startDate+
                "&stopDate="+stopDate+"&keyClassName="+keyClassName)
        },
        cancel:function() {
            $("#myDialog").dialog("close");
            transfer.dialog("close");
            lossDiv.dialog("close");
            transfer.dialog("close")
            transferForm.form("clear")
        }
        ,
        submit:function () {
            tabsForm.form("submit",{
                url:"/studentmanagement/update.do",
                onSubmit:function (param) {
                    param["grade.id"]=$("#gradeId").combobox("getValue")
                },
                success:function (data) {
                    data=$.parseJSON(data)
                    if(data.success){
                        $.messager.alert("温馨提示","编辑成功","info",function () {
                            methodObj.cancel();
                            empdata.datagrid("reload");
                        })
                    }else{
                        $.messager.alert("温馨提示",data.msg,"error");
                    }
                }
            })
        },
        submit2:function () {
                transferForm.form("submit",{
                    url:"/studentmanagement/updateClass.do",
                    onSubmit:function (param) {
                        param["state"]="转班状态"
                    },
                    success:function (data) {
                        data=$.parseJSON(data)
                        if(data.success){
                            $.messager.alert("温馨提示","编辑成功","info",function () {
                                methodObj.cancel();
                                empdata.datagrid("reload");
                            })
                        }else{
                            $.messager.alert("温馨提示",data.msg,"error");
                        }
                    }
                })
        },
        submit3:function () {
            var row=empdata.datagrid("getSelected");
            console.log(row);
            var grade=row.grade
            if(grade){
                var className=grade["className"]
            }
            lossForm.form("submit",{
                url:"/studentmanagement/updateLossState.do",
                onSubmit:function (param) {
                  param["tel"]=row.tel
                  param["className"]=className
                  param["state"]=0
                  param["name"]=row.name
                  param["username"]=row.salesman.username
                  param["qq"]=row.qq
                },
                success:function (data) {
                    data=$.parseJSON(data)
                    if(data.success){
                        $.messager.alert("温馨提示","编辑成功","info",function () {
                            methodObj.cancel();
                            empdata.datagrid("reload");
                        })
                    }else{
                        $.messager.alert("温馨提示",data.msg,"error");
                    }
                }
            })
            $.post("/studentmanagement/updateState.do",{"id":row.id,"state":"流失状态"},function () {
            },"json")
        }

    }
    $("a[data-click]").click(function () {
        var incident=$(this).data("click");
        methodObj[incident]();
    })
    $("#lossDiv").dialog({
        closed:true,
        width:300,
        height:150,
        onColse:function () {
            tabsForm.form("clear")
        },
        buttons:"#btns3"
    })

    transfer.dialog({
        closed:true,
        width:300,
        height:200,
        onColse:function () {
            transfer.form("clear")
        },
        buttons:"#btns2"
    })
    $("#keyState").combobox({
        onChange:function () {
            var keyState=$("#keyState").combobox("getValue");
            empdata.datagrid("load",{
                keyState:keyState
            })
        }
    })
})